<template>
  <link rel="stylesheet" href="assets/css/kickback.css" />
  <div class="headerbox">
    <div class="header">
      <div class="headerL"></div>
      <div class="headerC">
        <p>分销佣金</p>
      </div>
      <div class="headerR"></div>
    </div>
  </div>
  <div class="clear"></div>
  <div class="h50"></div>
  <div class="kickback_header">
    <div class="left">
      <div>
        可提现(元): <span>{{ money.cMoney }}</span>
      </div>
      <div>
        累计提现(元): <span>{{ money.inMoney }}</span>
      </div>
    </div>
    <div class="right" @click="cash">立即提现</div>
  </div>
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
      class="kickback"
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <div class="item" v-for="(item, index) in list" :key="index">
        <div class="left">
          <div class="img">
            <img :src="item.business.avatar_text" alt="" />
          </div>
          <div class="content">
            <div class="nickname">会员昵称：{{ item.business.nickname }}</div>
            <div class="tel">订单号：{{ item.order.code }}</div>
            <div class="tel">消费时间：{{ item.order.createtime_text }}</div>
          </div>
        </div>
        <div class="right">
          <div class="status" v-if="item.receive_status">已结算</div>
          <div class="status"  style="color:rgb(42, 164, 189)" v-else>待领取</div>
          <div class="money">+{{ item.amount }}</div>
        </div>
      </div>
    </van-list>
  </van-pull-refresh>
</template>
<script setup>
import { getCurrentInstance, onMounted } from "vue";

const { proxy } = getCurrentInstance();
const list = ref([]);
const count = ref(0);
const loading = ref(false);
const finished = ref(false);
const refreshing = ref(false);
const business = proxy.$business.value;

//提现与未提现
const money = ref({});

//列表查询
const load = async () => {
  var result = await proxy.$POST({
    url: "/team/moneylist",
    params: { busid: business.id },
  });

  if (result.code == 1) {
    count.value = result.data.count;
    list.value = result.data.list;
    list.value.map((item)=>{
      item.receive_status = item.status == '1' ? true : false
    })
    money.value = result.data.money;
    loading.value = false;
  }

  console.log(result.data)
};
//上拉加载
const onLoad = async () => {
  loading.value = true;
  //请求成功后清除旧的数据,并取消刷新状态
  if (refreshing.value) {
    list.value = [];
    refreshing.value = false;
  }
  load();

  if (list.value.length >= count.value) {
    finished.value = true;
  }
};

//下拉刷新
const onRefresh = () => {
  // 清空列表数据
  finished.value = true;

  // 重新加载数据
  onLoad();
};


//点击提现
const cash = async () => {
  //弹框确认
  proxy.$confirm({
      title: "提现确认",
      message: "是否确认全部提现",
    })
    .then(async () => {
      var result = await proxy.$POST({
        url: "/team/money",
        params: { busid: business.id },
      });

      if (result.code == 0) {
        proxy.$fail(result.msg);
        return;
      } else if (result.code == 1) {
        proxy.$success(result.msg)
        onRefresh()
        return
      }
    })
    .catch(() => {});
};
</script>